/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月4日
 */
package com.massyframework.beanskin.runtime.service.filting;

import java.util.Map;
import java.util.Objects;

import com.massyframework.beanskin.service.Filter;
import com.massyframework.beanskin.service.ServiceReference;

/**
 * 服务类型过滤器
 * 
 * <p>
 * 筛选符合的服务类型
 * 
 * @author huangkh
 *
 */
public final class ServiceTypeFilter implements Filter {

    private final Class<?> serviceType;
    private final Filter target;

    public ServiceTypeFilter(Class<?> serviceType, Filter target){
        this.serviceType =
                Objects.requireNonNull(serviceType, "\"serviceType\" cannot be null.");
        this.target =
                Objects.requireNonNull(target, "\"target\" cannot be null.");
    }

    /**
     * 对map进行过滤，对map的key和value进行不区分大小写匹配。
     *
     * @param props {@link Map}属性
     * @return {@code true}匹配，{@code false}不匹配
     */
    @Override
    public boolean match(Map<String, Object> props) {
        Class<?>[] serviceTypes = this.getServiceTypes(props);
        if (serviceTypes == null) return false;

        if (!this.isMatchWithServiceTypes(serviceTypes)){
            return false;
        }
        return this.getTagetFilter().match(props);
    }

    /**
     * 对服务引用进行过滤，对reference的属性名和属性值进行不区分大小写进行匹配
     *
     * @param reference {@link ServiceReference}
     * @return {@code true}匹配，{@code false}不匹配
     */
    @Override
    public <S> boolean match(ServiceReference<S> reference) {
        if (!this.isMatchWithServiceTypes(reference.getExportTypes())){
            return false;
        }
        return this.getTagetFilter().match(reference);
    }

    /**
     * 服务类型是否匹配
     * @param serviceTypes {@link Class}数组
     * @return {@link boolean}, 返回<code>true</code>表示匹配，否则返回<code>false</code>
     */
    protected boolean isMatchWithServiceTypes(Class<?>[] serviceTypes){
        for (Class<?> type: serviceTypes){
            if (this.serviceType == type){
                return true;
            }
        }

        return false;
    }

    /**
     * 对象类型
     * @param props 属性
     * @return {@link Class}数组
     */
    protected Class<?>[] getServiceTypes(Map<String, Object> props){
        return (Class<?>[])props.get(ServiceReference.EXPORT_TYPES);
    }


    /**
     * 目标{@link Filter}
     * @return {@link Filter}
     */
    public Filter getTagetFilter(){
        return this.target;
    }
}

